package org.surelink.radius.console;


import org.surelink.radius.cache.UserCache;
import org.surelink.radius.console.message.Request;
import org.surelink.radius.console.message.RespBase;
import org.surelink.radius.console.message.Response;
import org.surelink.radius.constant.UserStatus;
import org.surelink.radius.model.User;
import de.tototec.cmdoption.CmdOption;
import de.tototec.cmdoption.CmdlineParser;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class UserProcess extends BaseProcess
{


    @Autowired
    private UserCache userCache;

    class Config
    {
        @CmdOption(names = {"--help"}, description = "打印帮助", isHelp = true)
        private boolean help;

        @CmdOption(names = {"set"}, description = "设置用户")
        private boolean set;

        @CmdOption(names = {"get"}, description = "查询用户")
        private boolean get;

        @CmdOption(names = {"list"}, description = "查询用户列表")
        private boolean list;

        @CmdOption(names = {"limit"}, description = "查询用户数")
        private int limit = 100;

        @CmdOption(names = {"user"}, description = "用户命令", hidden = true)
        private boolean user;

        @CmdOption(names = {"--username", "-u"}, args = {"userName"}, minCount = -1, maxCount = 1,
                description = "用户上网账号")
        String username;

        @CmdOption(names = {"--password", "-p"}, args = {"password"}, minCount = -1, maxCount = 1,
                description = "用户上网密码")
        String password;

        @CmdOption(names = {"--groupname", "-g"}, args = {"groupName"}, minCount = -1, description = "组名")
        String groupname;


        @CmdOption(names = {"--status", "-s"}, args = {"status"}, minCount = -1, description = "状态:0:停机/1:正常")
        String status;

    }


    public UserProcess()
    {
        setOperate("user");
    }

    @Override
    public Response process(Request req) throws IOException
    {
        Config confg = new Config();
        CmdlineParser cp = new CmdlineParser(confg);
        cp.setProgramName(getOperate());
        cp.parse(req.getCommand().split("\\s+"));

        if (confg.help)
        {
            StringBuilder sb = new StringBuilder();
            cp.usage(sb);
            return new Response(RespBase.SUCCESS.getCode(), sb.toString());
        }

        if (confg.set)
        {
            if (confg.username == null || confg.password == null)
                return new Response(RespBase.MSG_FORMAT_ERR.getCode(), "用户名密码不能为空");

            User user = new User();
            user.setUserName(confg.username);
            user.setPassword(confg.password);
            user.setStatus(confg.status != null ? Integer.parseInt(confg.status) : UserStatus.Normal.value());
            user.setGroupName(confg.groupname != null ? confg.groupname : "default");
            user.setStatus(UserStatus.Normal.value());

            userCache.set(user);
            return RespBase.SUCCESS;

        }
        else if (confg.get)
        {
            if (confg.username == null)
                return new Response(RespBase.MSG_FORMAT_ERR.getCode(), "用户名不能为空");

            User user = userCache.get(confg.username);
            if (user == null)
                return new Response(RespBase.NOT_EXIST_ERR.getCode(), "用户不存在");

            return new Response(user);
        }
        else if (confg.list)
        {
            List<User> users = userCache.list(0, confg.limit);
            if (users == null)
                return new Response(RespBase.NOT_EXIST_ERR.getCode(), "用户列表为空");

            return new Response(users);
        }

        StringBuilder sb = new StringBuilder();
        cp.usage(sb);
        return RespBase.MSG_FORMAT_ERR;
    }
}
